﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JenkinsCodeBuid.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Serilog;

namespace JenkinsCodeBuid.Controllers
{
    public class BuildController : Controller
    {
        private static IConfiguration _configuration;
        private readonly ILogger<BuildController> _logger;
        public BuildController(IConfiguration configuration, ILogger<BuildController> logger)
        {
            _configuration = configuration;
            _logger = logger;
        }
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public async Task<ActionResult> GetAllSite([FromBody]A a)
        {
            try
            {
                if (a == null)
                    return Ok(new List<string>());
                if (string.IsNullOrWhiteSpace(a.file))
                    return Ok(new List<string>());
                if (a.file=="1")
                {
                    CommonConst.JenkinsUrl = _configuration.GetSection("PathMain").Value;
                }
                if (a.file == "2")
                {
                    CommonConst.JenkinsUrl = _configuration.GetSection("PathNew").Value;
                }
                var websitesUrl= _configuration.GetSection("WebsitesUrl").Value;

                var data =await RestClientHelper.PostData<List<string>>(websitesUrl,new { key=""});

                var list = new List<string>();
                return Ok(data);
                //data.ForEach(x => {
                //    list.Add(x.Split(':')[0]);
                //});

                ////var reslut=await FileHelper.GetFileNames(CommonConst.JenkinsUrl);
                ////var list = new List<string>();
                ////reslut.ForEach(x => {
                ////    var site = x.Replace(x, $"{x}.AppApi");
                ////    if(x.Contains("HysEC.Customers"))
                ////        site= x.Replace(x, $"HysEC.Customers.ClientWebApi");
                ////    site = site.Trim('\\');
                ////    list.Add(site);
                ////    site= x.Replace(x, $"{x}.WebApi");
                ////    if (x.Contains("HysEC.Customers"))
                ////        site = x.Replace(x, $"HysEC.Customers.WebApi");
                ////    site = site.Trim('\\');
                ////    list.Add(site);
                ////});
                ////CommonConst.AllWebSite = list;
                //CommonConst.AllWebSite = list;
                //return Ok(list);
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }
        }
        StringBuilder sb = new StringBuilder();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="websites"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<string> BuildWebsites([FromBody]List<string> webSites)
        {
            try
            {
                CommonConst.Msg = new System.Text.StringBuilder();
                CommonConst.Count = 0;
                sb = new StringBuilder();
                var baseUrl = _configuration.GetSection("BaseUrl").Value;
                if (!webSites.Any())
                {
                    throw new Exception("请选择一个站点");
                }
                foreach (var webSite in webSites)
                {
                    Log.Error($"{webSite}开始");
                    CommonConst.Msg.AppendLine("开始");
                    CommonConst.Website = webSite;
                    var site = webSite.Split(':')[0];
                    await BuildCodeHelper.BuildReleaseSite(site, _configuration, baseUrl);
                    CommonConst.Msg.AppendLine("发布成功");
                    var str = $@"{CommonConst.Website}已上传{CommonConst.Count}个文件---------------
                       {CommonConst.Msg.ToString()}%{baseUrl}";
                    sb.Insert(0,str);
                }
                return sb.ToString();
            }
            catch (Exception e)
            {
                sb.AppendLine(e.Message);
                //throw e;
                return sb.ToString();
            }

        }
        [HttpPost]
        public string GetMsg()
        {
            return $@"{sb}***************{CommonConst.Website}已上传{CommonConst.Count}个文件--------
                       {CommonConst.Msg.ToString()}";
            //+ "/r/rn错误" + CommonConst.ErrorMsg.ToString();
        }
    }
}
